<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
<html>    
<head>        
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">        
<title>Insert title here</title>        
</head>    
<script type="text/javascript">  

    var ws = null;      
    /* function log(text) {      
       document.getElementById("log").innerHTML = text +'\n'+document.getElementById("log").innerHTML;      
    }  */       
    function startServer() {        
      //  var url = document.getElementById("serverip").value;// "ws://192.168.0.102:8887";   
        var url ="ws://localhost:8080/websocketchat/hao/msg";  
        if ('WebSocket' in window) {        
            ws = new WebSocket(url);        
        } else if ('MozWebSocket' in window) {        
            ws = new MozWebSocket(url);        
        } else {        
            alert('浏览器不支持');        
            return;      
        }        
        ws.onopen = function() {        
            alert('Opened!');        
        };        
        // 收到服务器发送的文本消息, event.data表示文本内容        
        ws.onmessage = function(event) {        
            //alert('Receive message: ' + event.data);       
            //log('Receive message: ' + event.data);       
        };        
        ws.onclose = function() {        
          alert('Closed!');        
        };        
          
    }
    //发送信息      
    function sendMessage(){      
        var textMessage=document.getElementById("textMessage").value;      
              
        if(ws!=null&&textMessage!=""){      
            ws.send(textMessage);      
        }      
    }
    function shutServer(){
    	if(ws!=null){
    		ws.close();
    	}
    }
    
    </script>
    <body>
    kkkk
    <button id="createLink" onClick="startServer()">建立websocket连接</button>
    <input id="textMessage" />
    <button id="sendMessage" onClick="sendMessage()">发送消息</button>
    <button id="shutServer" onClick="shutServer()">关闭连接</button>
    <!-- <button id="shotLink" onClick="startServer()">建立websocket连接</button>
    <button id="createLink" onClick="startServer()">建立websocket连接</button> -->
    </body>